package com.api.devices.test.testMain;

import com.hierynomus.msfscc.fileinformation.FileIdBothDirectoryInformation;
import com.hierynomus.smbj.*;
import com.hierynomus.smbj.auth.*;
import com.hierynomus.smbj.connection.Connection;
import com.hierynomus.smbj.session.Session;
import com.hierynomus.smbj.share.*;
import java.util.*;

public class WindowsSmbPathExample {
    public static void main(String[] args) throws Exception {
        SMBClient client = new SMBClient();

        // Windows服务器连接信息
        String server = "172.16.0.94";  // 或主机名
        String shareName = "dat"; // 共享名称
//        String domain = "DOMAIN";         // 如果是域环境
        String domain = "";         // 如果是域环境
        String username = "minipc";
        String password = "anavi";

        try (Connection connection = client.connect(server)) {
            AuthenticationContext auth = new AuthenticationContext(
                    username, password.toCharArray(), domain);
            Session session = connection.authenticate(auth);

            // 连接到共享
            try (DiskShare share = (DiskShare) session.connectShare(shareName)) {
                // 示例1：列出根目录内容
                listDirectory(share, "\\");

                // 示例2：列出特定目录内容
                listDirectory(share, "000_000_003_001\\dat\\");
//
//                // 示例3：检查文件是否存在
                checkFileExists(share, "000_000_003_001\\dat\\20250615\\20250615103000.dat");
            }
        }
    }

    private static void listDirectory(DiskShare share, String path) {
        // 确保目录路径以反斜杠结尾
        if (!path.endsWith("\\")) {
            path += "\\";
        }

        System.out.println("列出目录: " + path);
        try {
            List<FileIdBothDirectoryInformation> files = share.list(path);
            for (FileIdBothDirectoryInformation file : files) {
                System.out.println("is file");
//                System.out.println((file.isDirectory() ? "[DIR] " : "[FILE]") + file.getFileName());
            }
        } catch (Exception e) {
            System.err.println("访问目录失败: " + e.getMessage());
        }
    }

    private static void checkFileExists(DiskShare share, String filePath) {
        System.out.println("检查文件是否存在: " + filePath);
        boolean exists = share.fileExists(filePath);
        System.out.println("文件 " + filePath + " 存在: " + exists);
    }
}